<template>
  <el-form label-width="100px">
    <el-form-item label="SKU名称">
      <el-input placeholder="SKU名称" v-model="skuParams.skuName"></el-input>
    </el-form-item>
    <el-form-item label="价格(元)">
      <el-input
        placeholder="价格(元)"
        type="number"
        v-model="skuParams.price"
      ></el-input>
    </el-form-item>
    <el-form-item label="重量(g)">
      <el-input
        placeholder="重量(g)"
        type="number"
        v-model="skuParams.weight"
      ></el-input>
    </el-form-item>
    <el-form-item label="SKU描述">
      <el-input
        placeholder="SKU描述"
        type="textarea"
        v-model="skuParams.skuDesc"
      ></el-input>
    </el-form-item>
    <el-form-item label="平台属性">
      <el-form label-width="80px" :inline="true">
        <el-form-item
          v-for="item in attrArr"
          :key="item.id"
          :label="item.attrName"
        >
          <el-select v-model="item.attrIdAndValueId" style="width: 120px">
            <el-option
              :value="`${item.id}:${attrValue.id}`"
              v-for="attrValue in item.attrValueList"
              :key="attrValue.id"
              :label="attrValue.valueName"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </el-form-item>
    <el-form-item label="销售属性">
      <el-form label-width="80px" :inline="true">
        <el-form-item
          v-for="item in saleArr"
          :key="item.id"
          :label="item.saleAttrName"
        >
          <el-select v-model="item.saleIdAndValueId" style="width: 120px">
            <el-option
              :value="`${item.id}:${saleAttrValue.id}`"
              v-for="saleAttrValue in item.spuSaleAttrValueList"
              :key="saleAttrValue.id"
              :label="saleAttrValue.saleAttrValueName"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </el-form-item>
    <el-form-item label="图片名称">
      <el-table border :data="imageArr" ref="table">
        <el-table-column
          type="selection"
          width="80px"
          align="center"
        ></el-table-column>
        <el-table-column label="图片">
          <template #default="{ row }">
            <!--            <img :src="row.imgUrl" :alt="row.imgName" style="width: 100%;height: 100%">-->
            <el-image
              :src="row.imgUrl"
              style="width: 100px; height: 100px"
              :zoom-rate="1.2"
              :max-scale="7"
              :min-scale="0.2"
              :preview-src-list="[row.imgUrl]"
              :initial-index="0"
              fit="cover"
              :preview-teleported="true"
              :alt="row.imgName"
            />
          </template>
        </el-table-column>
        <el-table-column label="名称"></el-table-column>
        <el-table-column label="操作">
          <template #default="{ row }">
            <el-button type="primary" size="small" @click="handler(row)">
              设置默认
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="save">保存</el-button>
      <el-button type="primary" @click="cancel">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script setup lang="ts">
import { reqAttr } from '@/api/product/attr'
import {
  SaleAttr,
  SaleAttrResponseData,
  SkuData,
  SpuData,
  SpuHasImage,
  SpuImage,
} from '@/api/product/spu/type.ts'
import { Attr, AttrList, AttrResponseData } from '@/api/product/attr/type.ts'
import {
  reqAddSku,
  reqSpuHasSaleAttr,
  reqSpuImageList,
} from '@/api/product/spu'
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus'

let $emit = defineEmits(['changeScene'])
const cancel = () => {
  $emit('changeScene', { flag: 0, params: '' })
}
// 平台属性
let attrArr = ref<AttrList>([])
// 销售属性
let saleArr = ref<SaleAttr[]>([])
// 照片
let imageArr = ref<SpuImage[]>([])
// 当前子组件的方法对外暴露
const initSkuData = async (
  c1Id: number | string,
  c2Id: number | string,
  spu: SpuData,
) => {
  // 收集上传用的数据
  skuParams.category3Id = spu.category3Id
  skuParams.spuId = spu.id as number
  skuParams.tmId = spu.tmId
  // 获取平台属性
  let result: AttrResponseData = await reqAttr(c1Id, c2Id, spu.category3Id)
  // 获取销售属性
  let result1: SaleAttrResponseData = await reqSpuHasSaleAttr(spu.id as number)
  // 获取照片墙的数据
  let result2: SpuHasImage = await reqSpuImageList(<number>spu.id)

  attrArr.value = result.data
  saleArr.value = result1.data
  imageArr.value = result2.data
}
// 收集SKU的参数
let skuParams = reactive<SkuData>({
  // 父组件传来了
  category3Id: '',
  spuId: '',
  tmId: '',
  // v-model去收
  skuName: '',
  price: '',
  weight: '',
  skuDesc: '',
  skuAttrValueList: [
    {
      attrId: '',
      valueId: '',
    },
  ],
  skuSaleAttrValueList: [
    {
      saleAttrId: '',
      saleAttrValueId: '',
    },
  ],
  skuDefaultImg: '',
})
// 获取table组件实例
let table = ref<any>()
// 设置默认图片的方法回调
const handler = (row: any) => {
  // 先让全部的复选框不勾选
  // imageArr.value.forEach((item: any) => {
  //   table.value.toggleRowSelection(item, false)
  // })
  table.value.clearSelection()
  //复选框选中
  table.value.toggleRowSelection(row, true)
  // 收集图片地址
  skuParams.skuDefaultImg = row.imgUrl
}
// 保存
const save = async () => {
  // 平台属性收集
  skuParams.skuAttrValueList = attrArr.value.reduce((prev: any, next: Attr) => {
    if (next.attrIdAndValueId) {
      let [attrId, valueId] = next.attrIdAndValueId.split(':')
      prev.push({ attrId, valueId })
    }
    return prev
  }, [])
  //销售属性收集
  skuParams.skuSaleAttrValueList = saleArr.value.reduce(
    (prev: any, next: SaleAttr) => {
      if (next.saleIdAndValueId) {
        let [saleAttrId, saleAttrValueId] = next.saleIdAndValueId.split(':')
        prev.push({ saleAttrId, saleAttrValueId })
      }
      return prev
    },
    [],
  )

  // 发请求
  let result = await reqAddSku(skuParams)
  if (result.code == 200) {
    ElMessage({
      type: 'success',
      message: '添加SKU成功',
    })
  } else {
    ElMessage({
      type: 'error',
      message: '添加SKU失败',
    })
  }
  //切换场景
  $emit('changeScene', { flag: 0, params: '' })
}
defineExpose({ initSkuData })
</script>

<style scoped></style>
